package com.study.samsung.Control;

import com.alibaba.fastjson.JSONObject;
import com.study.samsung.Dao.CollectDAO;
import com.study.samsung.Entity.Collect;
import com.study.samsung.Entity.Commodity;
import com.study.samsung.Entity.Succession;
import com.study.samsung.Entity.User;
import com.study.samsung.Service.CollectService;
import com.study.samsung.Service.GoodsService;
import com.study.samsung.Service.IndexSearchService;
import com.study.samsung.Utils.BaseServlet;
import com.study.samsung.Utils.Pager;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

//我的收藏
@WebServlet("/MyCollection")
public class MyCollectionServlet extends BaseServlet {
    CollectService cs = new CollectService();
    IndexSearchService iss = new IndexSearchService();
    GoodsService gds = new GoodsService();

    @Override
    public String excute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        List<Commodity> newgoods = iss.selectnew();
        List<Succession> selectxl = iss.selectxl();

        request.setAttribute("newgoods",newgoods);
        request.setAttribute("selectxl",selectxl);

        Object user = session.getAttribute("user");
        if (user!=null){
            User nowuser = (User) user;
            Integer uid = nowuser.getUid();

            session.setAttribute("cartcount",gds.cartcount(uid));
        }
        return "f:MyCollection";
    }
    public JSONObject selectAllCol(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        //设置响应内容的主体格式，是json格式的
        response.setContentType("application/json;charset=utf-8");
        User user = (User) session.getAttribute("user");
        System.out.println(user);
        List<Collect> list = cs.selectAllCol(user.getUid());
        Integer pageno =Integer.parseInt(request.getParameter("pageNo"));
        session.setAttribute("pageno",pageno);
        Integer pagesize = 3;
        Pager pager = cs.selectAllColfy(user.getUid(),pageno,pagesize);
        System.out.println(pager.getCollectfy());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("pager",pager);
        System.out.println(jsonObject);
        return jsonObject;
    }
    public String clickImg(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        //设置响应内容的主体格式，是json格式的
        response.setContentType("application/json;charset=utf-8");
        User user = (User) session.getAttribute("user");
        System.out.println(user);
        Integer pageno = (Integer) session.getAttribute("pageno");
        List<Collect> list = cs.selectAllImg(user.getUid(),pageno);
        String json = JSONObject.toJSONString(list);
        System.out.println(json);
        return json;
    }
    public String passSid (HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        //设置响应内容的主体格式，是json格式的
        response.setContentType("application/json;charset=utf-8");
        User user = (User) session.getAttribute("user");
        String id = request.getParameter("sid");
        Integer sid = Integer.parseInt(id);
        Collect collect = cs.selectOne(user.getUid(), sid);
        session.setAttribute("lid",collect.getLid());
        session.setAttribute("colsid",collect.getSid());
        System.out.println(collect.getLid());
        String json = JSONObject.toJSONString(collect);
        System.out.println(json);
        return json;
    }
}
